package com.longcai.piaojia.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.longcai.piaojia.R;
import com.longcai.piaojia.conn.ShowListGet;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.List;

/**
 * Created by Administrator on 2015/6/1.
 */
public class ShowListAdapter extends ArrayAdapter<ShowListGet.ShowEntity> {

    private LayoutInflater layoutInflater;
    private Context context;

    public ShowListAdapter(Context context, List<ShowListGet.ShowEntity> objects) {

        super(context, 0, objects);

        this.context = context;

        layoutInflater = ((Activity)context).getLayoutInflater();

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder;

        ShowListGet.ShowEntity showEntity = getItem(position);

        if(convertView == null){

            convertView = BoundViewHelper.boundView(viewHolder = new ViewHolder(), ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) layoutInflater.inflate(R.layout.item_search_result_list, null)));

            convertView.setTag(viewHolder);

        }else{

            viewHolder = (ViewHolder) convertView.getTag();

        }

        viewHolder.item_search_result_list_title_tv.setText(showEntity.title);
        viewHolder.item_search_result_list_time_tv.setText(showEntity.start_time);
        viewHolder.item_search_result_list_address_tv.setText(showEntity.place_name);

        switch (Integer.parseInt(showEntity.is_seat)) {

            case 0:

                viewHolder.item_search_result_list_is_seat_tv.setVisibility(View.GONE);
                viewHolder.item_search_result_list_undetermined_tv.setVisibility(View.GONE);

                break;

            case 1:

                viewHolder.item_search_result_list_is_seat_tv.setVisibility(View.VISIBLE);
                viewHolder.item_search_result_list_undetermined_tv.setVisibility(View.GONE);

                break;

            case 2:

                viewHolder.item_search_result_list_is_seat_tv.setVisibility(View.GONE);
                viewHolder.item_search_result_list_undetermined_tv.setVisibility(View.VISIBLE);

                break;

        }

        Log.e("-----", showEntity.status);

        if (showEntity.status.equals("0")) {

            viewHolder.item_search_result_cao_iv.setVisibility(View.VISIBLE);

        } else {

            viewHolder.item_search_result_cao_iv.setVisibility(View.GONE);

        }

        switch (Integer.parseInt(showEntity.status)) {

            case 0:

                viewHolder.item_search_result_list_book_tv.setTextColor(ContextCompat.getColor(context, R.color.hint_color));
                viewHolder.item_search_result_list_book_tv.setBackgroundResource(R.drawable.item_mycollection_nostart_text_bg);
                viewHolder.item_search_result_list_book_tv.setText(context.getResources().getString(R.string.goods_info_status_notstart_text));

                break;
            case 1:

                viewHolder.item_search_result_list_book_tv.setTextColor(ContextCompat.getColor(context, R.color.item_mycollection_money_text_color));
                viewHolder.item_search_result_list_book_tv.setBackgroundResource(R.drawable.item_mycollection_ordering_text_bg);
                viewHolder.item_search_result_list_book_tv.setText(context.getResources().getString(R.string.goods_info_status_ordering_text));

                break;
            case 2:

                viewHolder.item_search_result_list_book_tv.setTextColor(ContextCompat.getColor(context, R.color.item_mycollection_sale_text_color));
                viewHolder.item_search_result_list_book_tv.setBackgroundResource(R.drawable.item_mycollection_sale_text_bg);
                viewHolder.item_search_result_list_book_tv.setText(context.getResources().getString(R.string.goods_info_status_selling_text));

                break;
            case 3:

                viewHolder.item_search_result_list_book_tv.setTextColor(ContextCompat.getColor(context, R.color.hint_color));
                viewHolder.item_search_result_list_book_tv.setBackgroundResource(R.drawable.item_mycollection_nostart_text_bg);
                viewHolder.item_search_result_list_book_tv.setText(context.getResources().getString(R.string.goods_info_status_sellout_text));

                break;

        }

        viewHolder.item_search_result_list_price_tv.setText(showEntity.price_low + context.getResources().getString(R.string.goods_info_symbols_text)+showEntity.price_high + context.getResources().getString(R.string.goods_info_money_text));

        GlideLoader.getInstance().get(context, showEntity.picurl, viewHolder.item_search_result_list_iv, R.mipmap.goods);

        return convertView;

    }

    private class ViewHolder{

        @BoundView(R.id.item_search_result_list_iv) public ImageView item_search_result_list_iv;
        @BoundView(R.id.item_search_result_cao_iv) public ImageView item_search_result_cao_iv;
        @BoundView(R.id.item_search_result_list_title_tv) public TextView item_search_result_list_title_tv;
        @BoundView(R.id.item_search_result_list_time_tv) public TextView item_search_result_list_time_tv;
        @BoundView(R.id.item_search_result_list_address_tv) public TextView item_search_result_list_address_tv;
        @BoundView(R.id.item_search_result_list_is_seat_tv) public TextView item_search_result_list_is_seat_tv;
        @BoundView(R.id.item_search_result_list_undetermined_tv) public TextView item_search_result_list_undetermined_tv;
        @BoundView(R.id.item_search_result_list_book_tv) public TextView item_search_result_list_book_tv;
        @BoundView(R.id.item_search_result_list_price_tv) public TextView item_search_result_list_price_tv;

    }

}
